<template>
  <div class="app">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目名称" prop="inspection_name">
        <el-input
          v-model="queryParams.inspection_name"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="infoList">
      <el-table-column label="序号" align="center" prop="ins_id" />
      <el-table-column label="项目名称" align="center" prop="inspection_name" />
      <el-table-column label="项目类别" align="center" prop="class" />
      <el-table-column label="项目单价" align="center" prop="unit_price" />
      <el-table-column label="项目成本" align="center" prop="cost" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改处方附加费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序号" prop="ins_id"> 自动生成 </el-form-item>
        <el-form-item label="项目名称" prop="inspection_name">
          <el-input
            v-model="form.inspection_name"
            placeholder="请输入项目名称"
          />
        </el-form-item>
        <el-form-item label="项目类别" prop="class">
          <el-input v-model="form.class" placeholder="请输入项目类别" />
        </el-form-item>
        <el-form-item label="项目单价" prop="unit_price">
          <el-input v-model="form.unit_price" placeholder="请输入项目单价" />
        </el-form-item>
        <el-form-item label="项目成本" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入项目成本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo4, delInfo4, addInfo4, updateInfo4, selectByIdInfo4 } from "@/api/jjs/inspectionfee";
export default {
  data() {
    return {
      infoList: [],
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        inspection_name: null,
      },
      //表单参数
      form: {},
      //表单校验
      rules: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      listInfo4(this.queryParams).then((response) => {
        console.log(response)
        this.infoList = response.data;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.ins_id;
      console.log(ids);
      delInfo4(ids).then((response) => {
        this.getList();
        this.$alert("删除成功");
      });
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    //高级查询搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //添加取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //添加按钮标题
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检查项目费";
    },
    //修改按钮
    handleUpdate(row) {
      this.reset();
      const ins_id = row.ins_id || this.ins_id;
      selectByIdInfo4(ins_id).then((response) => {
        console.log(response.data[0]);
        this.form = response.data[0];
        this.open = true;
        this.title = "修改检查项目费";
      });
    },

    //添加，修改确定按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.ins_id != null) {
            updateInfo4(this.form).then((response) => {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.open = false;
              this.getList();
            });
          } else {
            addInfo4(this.form).then((response) => {
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
